package com.hetang.maijiayun.adaptertools.controller;

import com.hetang.maijiayun.adaptertools.config.MessageConfig;
import com.hetang.maijiayun.adaptertools.controller.vo.request.SyncRequest;
import com.hetang.maijiayun.adaptertools.controller.vo.response.MessageResponse;
import com.hetang.maijiayun.adaptertools.controller.vo.response.PlatformLostShops;
import com.hetang.maijiayun.adaptertools.controller.vo.response.Response;
import com.hetang.maijiayun.api.util.APIException;
import com.hetang.maijiayun.adaptertools.controller.vo.request.DetectorRequest;
import com.hetang.maijiayun.adaptertools.controller.vo.response.ShopLostOrder;
import com.hetang.maijiayun.adaptertools.service.IDetectorService;
import com.hetang.maijiayun.adaptertools.utils.JsonTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Administrator on 10/26/2015.
 */
@Controller
@RequestMapping("/ops/order")
public class OrderOpsController {
    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(OrderOpsController.class);
    /**
     * 订单服务service
     */
    @Autowired
    private IDetectorService detectorService;

    /**
     * 获取当前有漏单情况的门店 (参数:platform, startTime, endTime)
     *
     * @param request
     * @return
     */
    @RequestMapping("/checkshoporder")
    @ResponseBody
    public Response checkshoporder(@ModelAttribute DetectorRequest request) {
        long startTime = System.currentTimeMillis();
        logger.info("request time : {}, request param is : {}", System.currentTimeMillis(), JsonTools.createJsonString(request));
        if (null == request.getStartTime() || null == request.getEndTime()) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);
            request.setStartTime(calendar.getTime());
            calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - 30);
            request.setEndTime(calendar.getTime());
        }
        Response<PlatformLostShops> response = null;
        try {
            response = detectorService.findLostShops(request);
            return response;
        } catch (Exception e) {
            logger.error("获取丢失订单门店失败！！exception is : {}", e);
            response = new Response<PlatformLostShops>();
            MessageResponse messageResponse = new MessageResponse(MessageConfig.PARAM_FAIL_CODE, MessageConfig.INTERF_FAIL_CONTEXT);
            response.setResponse(messageResponse);
        }
        logger.debug("execute time : {}", System.currentTimeMillis() - startTime);
        return response;
    }

    /**
     * 按照平台补单
     *
     * @param request
     * @return
     */
    @RequestMapping("/syncOrders")
    @ResponseBody
    public MessageResponse syncOrders(@ModelAttribute SyncRequest request) {
        MessageResponse response = new MessageResponse(MessageConfig.SUCCESS_CODE, MessageConfig.SUCCESS_CONTEXT);
        try {
            response = detectorService.syncOrders(request);
        } catch (Exception e) {
            response.setMessageCode(MessageConfig.SYNC_FAIL_CODE);
            response.setMessageContext(MessageConfig.SYNC_FAIL_CONTEXT);
        }
        return response;
    }
}
